package com.service;

import com.entity.Commodity;
import com.entity.School;
import com.entity.SchoolCampus;
import com.entity.SchoolCampusDorm;
import com.mapper.CampusMapper;
import com.mapper.DormMapper;
import com.mapper.SchoolMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// 服务类
@Service
@Transactional
public class SchoolService {
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private CampusMapper campusMapper;
    @Autowired
    private DormMapper dormMapper;

    // ===================== 学校 ========================

    /**插入学校*/
    public Integer InsertSchool(School school){
        return schoolMapper.insertSchool(school);
    }
    /**查询学校*/
    public List<School> LookSchool(School school){
        return schoolMapper.lookSchool(school);
    }
    /**修改学校*/
    public Integer ChangeSchool(School school){
        return schoolMapper.changeSchool(school);
    }
    /**删除学校*/
    public Integer DeleteSchool(Integer id){
        int deleteStatus = schoolMapper.deleteSchool(id);
        if (deleteStatus > 0){
            // 删除成功，则尝试删除校区和宿舍
            campusMapper.deleteCampusBySchoolId(id);
            dormMapper.deleteDormBySchoolId(id);
        }
        return deleteStatus;
    }
    /**分页查询*/
    public List<School> QuerSchools(Integer page, Integer count, String schoolName){
        System.out.println(schoolName);
        return schoolMapper.querySchools(page,count,"%"+schoolName+"%");
    }
    /**统计学校总数*/
    public Integer QueryAllSchoolCount(String schoolName){
        return schoolMapper.queryAllSchoolCount(schoolName);
    }

    /** 获取所有学校*/
    public List<School> GetSchoolList(){
        return schoolMapper.getSchoolList();
    }
    // ===================== 校区 ========================

    /** 根据学校id获取校区*/
    public List<SchoolCampus> GetSchoolCampusList(Integer schoolId){
        return campusMapper.getSchoolCampusList(schoolId);
    }
    /**插入校区*/
    public Integer InsertCampus(SchoolCampus campus){
        return campusMapper.insertCampus(campus);
    }
    /**查询校区*/
    public List<SchoolCampus> LookCampus(SchoolCampus campus){
        return campusMapper.lookCampus(campus);
    }
    /**修改校区*/
    public Integer ChangeCampus(SchoolCampus campus){
        return campusMapper.changeCampus(campus);
    }
    /**删除校区*/
    public Integer DeleteCampus(Integer id){
        int deleteStatus = campusMapper.deleteCampus(id);
        if (deleteStatus > 0){
            // 删除成功，则尝宿舍
            dormMapper.deleteDormByCampusId(id);
        }
        return deleteStatus;
    }
    /**分页查询*/
    public List<SchoolCampus> QuerCampus(Integer page, Integer count, String campusName){
        return campusMapper.queryCampus(page,count,"%"+campusName+"%");
    }
    /**统计校区总数*/
    public Integer QueryAllCampusCount(String campusName){
        return campusMapper.queryAllCampusCount(campusName);
    }

    // ===================== 宿舍 ========================

    /** 根据校区id获取宿舍*/
    public List<SchoolCampusDorm> GetDormListByCampusId(Integer campusId){
        return dormMapper.getDormListByCampusId(campusId);
    }

    /**插入宿舍*/
    public Integer InsertDorm(SchoolCampusDorm dorm){
        return dormMapper.insertDorm(dorm);
    }
    /**查询宿舍*/
    public List<SchoolCampusDorm> LookDorm(SchoolCampusDorm dorm){
        return dormMapper.lookDorm(dorm);
    }
    /**修改宿舍*/
    public Integer ChangeDorm(SchoolCampusDorm dorm){
        return dormMapper.changeDorm(dorm);
    }
    /**删除宿舍*/
    public Integer DeleteDorm(Integer id){
        return dormMapper.deleteDorm(id);
    }
    /**分页查询*/
    public List<SchoolCampusDorm> QuerDorm(Integer page, Integer count, String dormName){
        return dormMapper.queryDorm(page,count,"%"+dormName+"%");
    }
    /**统计宿舍总数*/
    public Integer QueryAllDormCount(String dormName){
        return dormMapper.queryAllDormCount(dormName);
    }



}
